#ifndef ZOOMABLELABEL_H
#define ZOOMABLELABEL_H

#include <QLabel>
#include <QPixmap>
#include <QPoint>
#include <QMenu>
#include <QRect>

class ZoomableLabel : public QLabel
{
    Q_OBJECT
public:
    explicit ZoomableLabel(QWidget *parent = nullptr);

    void setPixmap(const QPixmap &pixmap);
    void resetZoom();
    void updateScaledPixmap();
    void setOriginalPixmap(const QPixmap &pixmap);

    // ROI相关功能
    void startROIDrawing();
    void stopROIDrawing();
    void clearROI();
    QRect getROI() const;
    bool hasROI() const;

    // 设置最大缩放限制
    void setMaxScaleFactor(float maxFactor);
    float maxScaleFactor() const;

signals:
    void roiDrawn(const QRect& roi);
    void roiCleared();

public slots:

protected:
    void paintEvent(QPaintEvent *event) override;
    void wheelEvent(QWheelEvent *event) override;
    void resizeEvent(QResizeEvent *event) override;
    void mouseDoubleClickEvent(QMouseEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent *event) override;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void contextMenuEvent(QContextMenuEvent *event) override;

private:
    QPixmap originalPixmap;
    float scaleFactor;
    float maxScaleFactorValue; // 最大缩放因子
    QPoint offset;
    QPoint lastMousePos;
    bool dragging;
    bool cacheDirty;
    QPixmap cachedPixmap;
    QMenu *contextMenu;

    // ROI绘制相关变量
    bool drawingROI;
    QPoint roiStartPoint;
    QPoint roiEndPoint;
    QRect currentROI;

    // 辅助函数
    QPoint windowToImage(const QPoint& windowPoint);
};

#endif // ZOOMABLELABEL_H
